from typing import List

from common.entitys.monitor import Monitor, MonitorType
from database.mapper import monitor_result
from utils import json_util, date_util


# 处理 task_c 结果数据
# 触发时间升序查询
def query_task_c_by_code(code) -> List[Monitor]:
    monitor_list = []
    task_c_trigger = monitor_result.select_by_code_type(code, MonitorType.four)
    for task_c_info in task_c_trigger:
        monitor_info = eval(json_util.format_str(task_c_info.get('monitor_result')))
        monitor_c = Monitor('', '')
        monitor_c.__dict__ = monitor_info
        monitor_list.append(monitor_c)
    return monitor_list


# 批量插入
def batch_insert_list_dict(insert_list, m_type: int, describe: str):
    monitor_list = []
    date_str = date_util.get_date_str()
    for insert_info in insert_list:
        if 'code' not in insert_info.keys():
            continue
        monitor_list.append({'code': insert_info['code'], 'monitor_date': date_str, 'monitor_name': describe,
                             'monitor_type': m_type, 'monitor_result': str(insert_info)})
    return monitor_result.insert_mul(monitor_list)
